﻿

#region Using Statements

using UnityEngine;
using System.Collections.Generic;

#endregion


/// <summary>
/// 
/// </summary>
public class JetPack : MonoBehaviour
{

    #region Members

    public Rigidbody2D body { get { return this.GetComponentInParent<Rigidbody2D>(); } }

    public bool inputActive { get; set; }

    public bool isActivated { get; private set; }

    public float fuel { get; set; }

    public float duration = 2.0f;

    public float power = 1000.0f;

    public float thresholdInitiate = 0.25f;

    public float rechargeTime = 5.0f;

    public ParticleSystem particles;

    #endregion


    #region Init

	/// <summary>
    /// 
    /// </summary>
    protected virtual void Awake()
    {
        fuel = 1.0f;
    }


    /// <summary>
    /// 
    /// </summary>
    protected virtual void Start()
    {

    }

    #endregion


    #region Update

    /// <summary>
    /// 
    /// </summary>
    protected virtual void FixedUpdate()
    {
        inputActive = Input.GetButton("Fire2");
        

        // if jetpack on
        if (isActivated)
        {
            if (!inputActive)
            {
                isActivated = false;
            }
            else
            {
                body.AddForce(power * Vector3.up * Time.deltaTime);
                fuel -= Time.deltaTime / duration;

                if (fuel <= 0.0f)
                {
                    isActivated = false;
                    fuel = 0.0f;
                }
            }
        }


        // if jetpack off
        else
        {
            if (inputActive && fuel > thresholdInitiate)
                isActivated = true;

            fuel = Mathf.Clamp(fuel + Time.deltaTime / rechargeTime, 0.0f, 1.0f);
        }


        // do particles
        ParticleSystem sys = particles == null ? particleSystem : particles;
        if (sys != null)
        {
            if (isActivated) sys.Play(true);
            else sys.Stop(true);
        }
    }

    #endregion

}